<?php
  //This has to be the first line of code a file if sessions are used.
  session_start();
  
  require_once ('check_PDO.php');
  require_once ('appinfo.php');

  if(isset($_SESSION['AppInfo'])) {
    $app = unserialize($_SESSION['AppInfo']);
    if($app->ConnURL == "") {
      header("Location: login.php"); 
      exit; 
    }
  } else {
    header("Location: login.php");
    exit; 
  }

  //Get DB connection info from session data
  include_once ('init_db_connect.php');  

  try {
    //cubrid:host=localhost;port=33000;dbname=demodb
    $conn_str = "cubrid:dbname=".$dbConfig['DB_DATABASE_NAME'].";host=".$dbConfig['DB_HOST'].";port=30000";
    $conn = new PDO($conn_str, $dbConfig['DB_USERNAME'], $dbConfig['DB_PASSWORD'] );
  } catch(PDOException $e) {
    header("Location: login.php"); 
  }

  require_once ('DBSchema.php');  

  $script = "";
  
  $use_auto_increment = "";
  $delimiter = "";
  
  if(isset($_GET['object_name']) && isset($_GET['object_type'])) {
    $object_name = $_GET['object_name'];
    $object_type = $_GET['object_type'];
    if(isset($_GET['delimiter'])) {
      $delimiter = $_GET['delimiter'];
    }
    if(isset($_GET['use_auto_increment'])) {
      $use_auto_increment = $_GET['use_auto_increment'];
    }

    switch($object_type) {
      case "table":
        $script = get_table_definition($conn, $object_name, $delimiter, $use_auto_increment);
      break;

      case "view":
        $script = get_view_definition($conn, $object_name, $delimiter);
      break;

      case "procedure":
        $script = get_procedure_definition($conn, $object_name, $delimiter);
      break;

      case "trigger":
        $script = get_trigger_definition($conn, $object_name, $delimiter);
      break;

      case "serial":
        $script = get_serial_definition($conn, $object_name, $delimiter);
      break;

      case "user":
        $script = get_user_definition($conn, $object_name, $delimiter);
        $script .= PHP_EOL;
        $script .= get_user_authorizations($conn, $object_name, $delimiter);
      break;

      default:
        exit;
    }
  } else {
    exit; 
  }  

  if($script == "") {
    exit;  
  } 

  $script = str_replace(" ,", ",", $script);
  $script = "--".PHP_EOL."-- ".ucfirst ($object_type)." ".$object_name.PHP_EOL."--".PHP_EOL.$script.PHP_EOL;
?>

<html>
  <head>
    <title>SQL Script for <?php echo $object_type; ?>: <?php echo $object_name; ?></title>
    <link rel="stylesheet" href="css/default.css">
    <link href='http://alexgorbatchev.com/pub/sh/2.0.296/styles/shCore.css' rel='stylesheet' type='text/css'/> 
    <link href='http://alexgorbatchev.com/pub/sh/2.0.296/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/> 
    <script src='http://alexgorbatchev.com/pub/sh/2.0.296/scripts/shCore.js' type='text/javascript'></script>
    <script src='http://alexgorbatchev.com/pub/sh/2.0.296/scripts/shBrushSql.js' type='text/javascript'></script>
    <script type='text/javascript'>
       SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/2.0.296/scripts/clipboard.swf';
       SyntaxHighlighter.all();
    </script> 
  </head>
  <body bgcolor="#F7F7F7">
    <br />
    <p align="center" class="title2">SQL Script for <?php echo $object_type; ?>: <font color="red"><?php echo $object_name; ?></font></p>
    <div style="width: 100%; padding: 20px;">
      <pre class="brush:[sql]"><?php echo $script; ?></pre>
    </div>
  </body>
</html>
